package org.tity.test.dao;

import java.util.ArrayList;
import java.util.List;

import org.tity.sql.dao.MySqlDAO;
import org.tity.sql.dao.OracleDAO;
import org.tity.sql.dao.SQLServerDAO;
import org.tity.sql.dao.SQLiteDAO;
import org.tity.sql.dao.parameter.MapParamBuilder;
import org.tity.sql.data.QueryMetaData;
import org.tity.sql.data.Rcd;
import org.tity.sql.data.RcdSet;
import org.tity.sql.data.SaveMode;
import org.tity.sql.sql.builder.SE;
import org.tity.test.entity.raw.sys.User;
import org.tity.test.entity.raw.sys.UserSet;

import com.alibaba.druid.pool.DruidDataSource;


public class SimpleDAODemo {

	/**
	 * 自定义的DAO类，因为连接的是MySQL，继承自 MySqlDAO 
	 * */
	public static class MyDAO extends MySqlDAO{}
	public static class MyDAO1 extends OracleDAO{}
	public static class MyDAO2 extends SQLServerDAO{}
	public static class MyDAO3 extends SQLiteDAO{};
	/**
	 * 创建连接池
	 * */
	public static  DruidDataSource createDataSource() {
		
		DruidDataSource dataSource=new DruidDataSource();
    	dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    	dataSource.setUsername("tity");
    	dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/titydb_test?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&autoReconnect=true");
    	dataSource.setPassword("tity2018");
    	return dataSource;
	}
	
	public static void main(String[] args) {
		//创建DAO实例
		MyDAO dao=new MyDAO();
		//创建数据源
		DruidDataSource ds=createDataSource();
		//设置数据源
		dao.setDataSource(ds);
		//执行查询
		String name="na";
		//查询并返回记录集
		RcdSet rs=dao.query("select * from sys_user where account like ?","%"+name+"%");
		System.out.println(rs.toString());
//		Rcd r2=rs.getRcd(2);
//		String userName=r2.getString("name");
//		Integer age=r2.getInteger("AGE");
//		r2.setValue("name", "李方捷");
		//循环数据
		for (Rcd r : rs) {
			System.out.println(r.getString("name"));
			System.out.println(r.toJSONObject());
		}
		
		//Lambda遍历
		rs.stream().forEach(r->{
			System.out.println("Lambda:"+r.toString());
		});
		
		//增加列
		rs.addColumn("newColumn");
		//填充列数据
		rs.stream().forEach(r-> {
			r.setValue("newColumn", "new_column_value");
		});
		//删除列
		rs.removeColumn("newColumn");
		
//		Object[] arr=rs.toArray("name");
//		Integer[] ints=rs.toIntArray("age");
//		List<Rcd> list=rs.toList();
//		List<Object> names=rs.toList("name");
//		Map<Object,Rcd> map=rs.toMap("name","age");
		
		
//		RcdSet rs1=rs.clone();
		
//		RcdSet rsd=rs.distinct("name","badge");
//		rs.rank("PaiMing","age",true);
	 
		RcdSet age18=rs.filter("age", 18);
		//rs.sort("age", "ASC", true);
		
//		RcdSet sub=rs.subset(2,20, true);
//		UserSet users=rs.toEntitySet(UserSet.class);
//		List<PojoUser> pojousers=rs.toPOJOSet(PojoUser.class);
//		Rcd r2=rs.getRcd(2);
//		User user=r2.toEntity(User.class);
		User sample=new User();
		sample.setAreaType("1").setCardType("vip");
		UserSet users=  dao.queryEntitySet(sample);
		
		User user=new User();
		user.generateId();
		long uderId=user.getId();
		user.setAccount("leefangjie");
		
		
		boolean suc=dao.insertEntity(user);
		
		suc=dao.updateEntity(user,SaveMode.DIRTY_FIELDS);
		
		suc=dao.saveEntity(user, SaveMode.ALL_FIELDS);
		
		
		
		User user1=new User();
		user1.generateId();
		user1.setAccount("leefangjie-1");
		
		User user2=new User();
		user2.generateId();
		user2.setAccount("leefangjie-2");
		
		UserSet us=new UserSet();
		us.addEntity(user1);
		us.addEntity(user2);
		
		List<User> ulist=new ArrayList<User>();
		ulist.add(user1);
		ulist.add(user2);
		
		//插入多个实体
		int[] r1=dao.insertEntitys(user1,user2);
		//插入实体集合
		int[] r2=dao.insertEntitys(us);
		//插入实体列表
		int[] r3=dao.insertEntitys(ulist);
		
		//更新多个实体
		int[] r11=dao.updateEntitys(SaveMode.ALL_FIELDS,user1,user2);
		//更新实体集合
		int[] r22=dao.updateEntitys(us,SaveMode.DIRTY_FIELDS);
		//更新实体列表
		int[] r33=dao.updateEntitys(ulist,SaveMode.NOT_NULL_FIELDS);
		
		//保存多个实体
		int[] r111=dao.saveEntitys(SaveMode.ALL_FIELDS,user1,user2);
		//保存实体集合
		int[] r222=dao.saveEntitys(us,SaveMode.DIRTY_FIELDS);
		//保存实体列表
		int[] r333=dao.saveEntitys(ulist,SaveMode.NOT_NULL_FIELDS);
		 
		//以sample的查询条件删除多个实体
		int count=dao.deleteEntitys(sample);
		//以user的PK值作为条件删除，删除单个实体
		boolean suc1=dao.deleteEntityByPK(user);
		//以PK值作为条件删除，删除多个实体
		int[] ret2=dao.deleteEntitysByPK(user1,user2);
		//以PK值作为条件删除，删除多个实体
		int[] ret3=dao.deleteEntitysByPK(ulist);
		//以PK值作为条件删除，删除多个实体
		int[] ret4=dao.deleteEntitysByPK(us);
		
		
		
		//设置3个参数 name，age，address
		MapParamBuilder ps=new MapParamBuilder();
		ps.sets("name","leefj","age",25).set("address", "ningbo");
 
		SE se=new SE("select * from table where name=:name and age=:age and address=:address", ps.map());
		
		System.out.println(se);
		
		 int pageSize=100;
		 int pageIndex=1;
		 RcdSet rsPaged =dao.queryPage("select * from sys_user where account like ?", pageSize, pageIndex,"%"+name+"%");
		 
		 Rcd r=dao.queryRecord("select * from sys_user where account like ?","%"+name+"%");
		 
		 QueryMetaData queryMeta=rs.getMetaData();
		 
		 
		
		 
		 
		 
	}
	
	
}
